package com.ordos.recruit.module.apply.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ordos.recruit.aop.anno.UnInternetApi;
import com.ordos.recruit.module.apply.entity.*;
import com.ordos.recruit.module.apply.service.RecruitApplyService;
import com.wonders.commons.util.exception.ApiException;
import com.wonders.commons.util.model.ValidatorResult;
import com.wonders.commons.util.response.ApiResponse;
import com.wonders.commons.validation.ObjectValidator;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 招聘申报Controller
 *
 * @author jin
 */
@RestController
@RequestMapping("apply")
public class RecruitApplyController {

    private final RecruitApplyService recruitApplyService;
    private final ObjectValidator objectValidator;

    public RecruitApplyController(RecruitApplyService recruitApplyService, ObjectValidator objectValidator) {
        this.recruitApplyService = recruitApplyService;
        this.objectValidator = objectValidator;
    }

    /**
     * 保存或修改申报数据接口。
     *
     * @return ApiResponse
     */
    @PostMapping("saveOrUpdate")
    public ApiResponse saveOrUpdate(@RequestBody RecruitApplyDto recruitApplyDto) {
        ValidatorResult<RecruitApplyDto> validatorResult = objectValidator.objectValidator(recruitApplyDto);
        if(validatorResult.isHasErrors()) {
            throw new ApiException(ApiResponse.dataError(0, validatorResult.getError()));
        }
        return recruitApplyService.saveOrUpdate(recruitApplyDto);
    }

    @PostMapping("view")
    public ApiResponse<RecruitApplyPo> view(@RequestBody RecruitApplyDto recruitApplyDto) {
        return ApiResponse.success(recruitApplyService.view(recruitApplyDto));
    }

    @PostMapping("allView")
    @UnInternetApi
    public ApiResponse<ViewRecruitApplyPo> allView(@RequestBody RecruitApplyDto recruitApplyDto) {
        return ApiResponse.success(recruitApplyService.allView(recruitApplyDto));
    }

    @PostMapping("getList")
    public ApiResponse<List<Map<String, String>>> getList(@RequestBody RecruitApplyDto recruitApplyDto) {
        return ApiResponse.success(recruitApplyService.getList(recruitApplyDto));
    }

    @PostMapping("getAllList")
    @UnInternetApi
    public ApiResponse<IPage<SpeRecruitApplyPo>> getAllList(@RequestBody SpeRecruitApplyDto speRecruitApplyDto) {
        IPage<SpeRecruitApplyPo> pageResult = recruitApplyService.getAllList(speRecruitApplyDto);
        return ApiResponse.success(pageResult);
    }
}
